import 'package:ble_printer/bean/ble_device.dart';
import 'package:ble_printer/ble_printer.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

///
///  ble_printer
///  shared_utils.dart
///
///  Created by kayoxu on 2021/6/2 at 3:19 下午
///  Copyright © 2021 kayoxu. All rights reserved.
///

class BaseSharedUtils {
  static setBleDevice(BleDevice data) {
    _set('ble_device', BlePrinter.share.toJson(data));
  }

  static Future<BleDevice?> getBleDevice() async {
    var data = await _getString('ble_device') ?? '';
    return BleDevice.fromJson(BlePrinter.share.getMapOrList(data));
  }

  ///base
  static _set(String key, value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if (value is String) {
      prefs.setString(key, value);
    } else if (value is int) {
      prefs.setInt(key, value);
    } else if (value is double) {
      prefs.setDouble(key, value);
    } else if (value is bool) {
      prefs.setBool(key, value);
    }
  }

  static Future<String?> _getString(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var string = prefs.getString(key);
    return string ?? '';
  }
}
